Skip to content

mcp: insert server and tool names into generated handlers with repr() - #540

Merged
zfoong merged 1 commit into
CraftOS-dev:devfrom
rohantiwari9573:fix/mcp-handler-name-quoting
Nov 24, 2026
Merged

zfoong merged 1 commit into
CraftOS-dev:devfrom
rohantiwari9573:fix/mcp-handler-name-quoting

Conversation

@rohantiwari9573

@rohantiwari9573 rohantiwari9573 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The handler for each MCP tool is generated as Python source. Server and tool names were inserted into that source between double quotes, but a tool's name is chosen by the MCP server. Names containing a quote, a newline, a backslash or a triple quote did not survive the round trip as plain strings.

This change inserts every name with repr(), which always produces a valid string literal, so each name is kept exactly as given. That includes the handler's docstring. Action names and MCP calls are unchanged.

Changes

  • agent_core/core/impl/mcp/adapter.py: create_mcp_handler_with_source builds the docstring and the _server / _tool values with repr().
  • tests/test_mcp_adapter_names.py (new, 5 tests): names with a quote, a quote and newlines, a triple quote, and a backslash line continuation. For each one, the generated source compiles, the names appear unchanged as constants of the handler, and defining and calling the handler has no other effect. A registered action whose tool has a quote in its name keeps that name.

Testing

Check Result
tests/test_mcp_adapter_names.py, Python 3.10 and 3.14 5 passed
Same tests against the previous adapter 5 failed (as expected)
All tests/test_mcp*.py, Python 3.10 and 3.14 48 passed, 8 skipped (POSIX-only stdio cases on Windows)
ruff check and ruff format clean
mypy no new errors (126 before and after)

MCP tool names are chosen by the MCP server and can contain any
character, including quotes, backslashes and newlines. Build the
generated handler source with repr() literals, so every server and
tool name is kept exactly as a string, in the docstring as well as in
the values the handler uses. Action names and calls are unchanged.

Adds tests with names containing quotes, newlines, triple quotes and a
line continuation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants